package action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import model.dto.Article;
import model.dto.User;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import core.db.DBTool;
import core.db.SysConfig;

public class ArticleAction extends ActionSupport{
	private static final DateFormat format = new SimpleDateFormat("yyyy-MM-dd") ;
	private String res ;
	private Article article ;
	private List<Article> articles ;
	public List<Article> getArticles() {
		return articles;
	}

	public void setArticles(List<Article> articles) {
		this.articles = articles;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}
	
	public String getContent(){
		try{
			String articleId = article.getArticleId() ;
			article = DBTool.getResult(new Article(), "select * from cdarticle where articleId = '"
					+ articleId + "'") ;
		}catch(Exception e){
			e.printStackTrace() ;
		}
		return "content" ;
	}
	public String listTitleInfo(){
		try{
			articles = DBTool.getResults(new Article(), "select title,articleId from cdarticle") ;
		}catch(Exception e){
			e.printStackTrace() ;
		}
		return "listTitleInfo" ;
	}
	/**
	 * 请求所有文章列表
	 * @return
	 */
	public String list(){
		try{
			articles = DBTool.getResults(new Article(), "select title,articleId from cdarticle") ;
		}catch(Exception e){
			e.printStackTrace() ;
		}
		return "list" ;
	}
	
	/**
	 * 添加新文章
	 * @return
	 */
	public String add(){
		try{
			initArticle();
			String sql = "insert into cdArticle (	articleId ,title,inputTime,content ,summary ,userId) values ('"
					+ article.getArticleId()
					+ "','"
					+ article.getTitle()
					+ "','"
					+ article.getInputTime()
					+ "','"
					+ article.getContent()
					+ "','"
					+ article.getSummary() + "','" + article.getUserId() + "') ";
			DBTool.update(sql) ;
			res="发表成功!" ;
		}catch(Exception e){
			e.printStackTrace() ;
		}
		return "add" ;
	}

	private void initArticle() {
		article.setUserId(getUserId()) ;
		article.setArticleId(getArticleId()) ;
		article.setInputTime(getInputTime()) ;
	}
	
	private String getInputTime() {
		Date d = new Date() ;
		return format.format(d) ;
	}

	//获得数据库中分配的ID
	private String getArticleId() {
		//首先获得最大的ID
		Article r = DBTool.getResult(new Article(), "select  * from cdarticle  where articleid = (select max(articleid)  as articleid from cdarticle )") ;
		//生成新的ID
		if(r==null)return "0000" ;
		return getNewArticleId(r.getArticleId());
	}
	//生成新的ID
	private String getNewArticleId(String maxId){
		int max = Integer.parseInt(maxId) ;
		/**
		 * 这里只写了4位数的算法，以后注册用户多的话需要修改
		 */
		max++ ;
		if(max<10) return "000" + max ;
		if(max<100) return "00" + max ;
		if(max<1000) return "0" + max ;
		return ""+max ;
	}
	/**
	 * 获取当前用户ID，如果无则返回匿名
	 * @return
	 */
	private String getUserId() {
		User u = (User)ServletActionContext.getContext().getSession().get(SysConfig.USER_KEY) ;
		if(u==null)return "000" ;//000为匿名的ID
		return u.getUserId();
	}

	/**
	 * 更新文章
	 * @return
	 */
	public String update(){
		
		return "update" ;
	}

	public static DateFormat getFormat() {
		return format;
	}

	public String getRes() {
		return res;
	}

	public void setRes(String res) {
		this.res = res;
	}
}
